<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产总数" prop="produceTotal">
        <el-input
          v-model="queryParams.produceTotal"
          placeholder="请输入生产总数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划生产日期" prop="planProduceDate" label-width="100px">
        <el-date-picker clearable
          v-model="queryParams.planProduceDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划日期生产数" prop="planProduceCount" label-width="110px">
        <el-input
          v-model="queryParams.planProduceCount"
          placeholder="请输入计划日期生产数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线体代码" prop="productLineCode" label-width="100px">
        <el-input
          v-model="queryParams.productLineCode"
          placeholder="请输入生产线体代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品缩写" prop="productAddr">
        <el-input
          v-model="queryParams.productAddr"
          placeholder="请输入产品缩写"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="软件代码" prop="softwareCode">
        <el-input
          v-model="queryParams.softwareCode"
          placeholder="请输入软件代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="硬件代码" prop="hardwareCode">
        <el-input
          v-model="queryParams.hardwareCode"
          placeholder="请输入硬件代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拓展1" prop="expand1">
        <el-input
          v-model="queryParams.expand1"
          placeholder="请输入拓展1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拓展2" prop="expand2">
        <el-input
          v-model="queryParams.expand2"
          placeholder="请输入拓展2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:produce-plan:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:produce-plan:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['produce:produce-plan:removed']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['produce:produce-plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProducePlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="物料号" align="center" prop="materialLot" />
      <el-table-column label="生产总数" align="center" prop="produceTotal" />
      <el-table-column label="计划生产日期" align="center" prop="planProduceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planProduceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产线体代码" align="center" prop="productLineCode" />
      <el-table-column label="产品缩写" align="center" prop="productAddr" />
      <el-table-column label="软件代码" align="center" prop="softwareCode" />
      <el-table-column label="硬件代码" align="center" prop="hardwareCode" />
      <el-table-column label="拓展1" align="center" prop="expand1" />
      <el-table-column label="拓展2" align="center" prop="expand2" />
      <el-table-column label="计划生产日期，生产数" align="center" prop="planProduceCount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:produce-plan:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:produce-plan:removed']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="物料号" prop="materialLot">
          <el-input v-model="form.materialLot" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="生产总数" prop="produceTotal">
          <el-input v-model="form.produceTotal" placeholder="请输入生产总数" />
        </el-form-item>
        <el-form-item label="计划生产日期" prop="planProduceDate" >
          <el-date-picker clearable
            v-model="form.planProduceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划日期生产数" prop="planProduceCount">
          <el-input v-model="form.planProduceCount" placeholder="请输入计划生产日期的生产数" />
        </el-form-item>
        <el-form-item label="生产线体代码" prop="productLineCode">
            <el-select 
              v-model="form.productLineCode" 
              placeholder="请选择生产线体代码" 
              clearable
            >
              <el-option 
              v-for="dict in dict.type.product_line_code" 
              :key="dict.value" 
              :label="dict.value+'-----'+dict.label"
              :value="dict.value" 
              />
            </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <!-- <el-input v-model="form.productType" placeholder="请输入产品缩写" /> -->
          <el-select
            v-model="form.productTypeCode"
            placeholder="请选择产品类型"
            @change="getProductAbbrList($event)"
            clearable
            filterable
           >
            <el-option
              v-for="item in productTypeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品缩写" prop="productAddr">
          <!-- <el-input v-model="form.productAddr" placeholder="请输入产品缩写" /> -->
          <el-select
            v-model="form.productAddr"
            placeholder="请选择产品缩写"
            clearable
            filterable
           >
            <el-option
              v-for="item in productAbbrList"
              :key="item.value"
              :label="item.value + '-----'+item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="软件代码" prop="softwareCode">
          <el-select 
            clearable=""
            v-model="form.softwareCode" 
            filterable 
            placeholder="请选择软件代码"
            >
            <el-option
              v-for="item in softWareCodeList"
              :key="item.value"
              :label="item.value +'-----'+item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="form.softwareCode" placeholder="请输入软件代码" /> -->
          
        </el-form-item>
        <el-form-item label="硬件代码" prop="hardwareCode">
          <el-select 
            clearable=""
            v-model="form.hardwareCode" 
            filterable 
            placeholder="请选择硬件代码"
            >
            <el-option
              v-for="item in hardWareCodeList"
              :key="item.value"
              :label="item.value +'-----'+item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col>
            <el-form-item label="拓展1" prop="expand1" label-width="140px" style="float: left;">
              <el-input v-model="form.expand1" placeholder="拓展1" style="width: 80px;" maxlength="1"/>
            </el-form-item>
            <el-form-item label="拓展2" prop="expand2" label-width="140px" style="float: left;">
              <el-input v-model="form.expand2" placeholder="拓展2" style="width: 80px;" maxlength="1"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductType } from "@/api/produce/product-type";
import { listProductHardwareInfo } from "@/api/produce/product-hardware-info";
import { listProductSoftwareRelease } from "@/api/produce/product-software-release";
import { listProducePlan, getProducePlan, removedProducePlan, addProducePlan, updateProducePlan } from "@/api/produce/produce-plan";
import { listProductInfo } from "@/api/produce/product-info";

export default {
  name: "Produce-plan",
  dicts: ['product_line_code'],
  data() {
    return {
      // 产品缩写代码
      productAbbrList: [],
      // 产品类型代码
      productTypeList: [],
      // 软件代码
      softWareCodeList: [],
      // 硬件代码
      hardWareCodeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划表格数据
      ProducePlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        produceTotal: null,
        planProduceDate: null,
        productLineCode: null,
        productAddr: null,
        softwareCode: null,
        hardwareCode: null,
        expand1: null,
        expand2: null,
        planProduceCount: null,
        removed: null,
        orderByColumn:'id',
          isAsc:'desc'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        produceTotal: [
          { required: true, message: "生产总数不能为空", trigger: "blur" }
        ],
        planProduceDate: [
          { required: true, message: "计划生产日期不能为空", trigger: "blur" }
        ],
        productLineCode: [
          { required: true, message: "生产线体代码不能为空", trigger: "blur" }
        ],
        productAddr: [
          { required: true, message: "产品缩写不能为空", trigger: "blur" }
        ],
        softwareCode: [
          { required: true, message: "软件代码不能为空", trigger: "blur" }
        ],
        hardwareCode: [
          { required: true, message: "硬件代码不能为空", trigger: "blur" }
        ],
        expand1: [
          { required: true, message: "拓展1不能为空", trigger: "blur" }
        ],
        expand2: [
          { required: true, message: "拓展2不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // console.log(this.dict.type.sys_job_group)
    this.getList();
    this.getHardWareList(); //获取硬件下拉列
    this.getSoftWareList(); //获取软件下拉列
    this.getProductType();  //获取产品类型下拉列
  },
  methods: {
    /** 查看产品信息-获得产品缩写列 */
    getProductAbbrList(){
      // 初始化
      this.productAbbrList = []
      const params = {
        pageNum: 1,
        pageSize: 10000,
        productTypeCode: this.form.productTypeCode
      }
      listProductInfo(params).then((res) => {
        console.log("产品信息",res)
        if(res.code === 200){
          if(res.rows.length > 0){
            for(var i=0;i<res.rows.length;i++){
              let temp = {
                value : res.rows[i].productAbbr,
                label : res.rows[i].productCode
              }
              this.productAbbrList.push(temp)
            }
          }
        }
      })
    },
    /** 查询产品类型下拉 */
    getProductType () {
      const params = {
        pageNum: 1,
        pageSize: 10000
      }
      listProductType(params).then(response => {
        this.productTypeList = response.rows;
        // 下拉框赋默认值
        this.form.productTypeCode = this.productTypeList[0].code;
        // 再去查询列表
        this.getProductAbbrList();
      }).catch((err) =>{
        this.$message.error("产品类型列表获取失败！，原因："+err)
      })
    },
    /** 获取硬件代码列表 */
    getSoftWareList(){
      const params={
        pageSize:10000,
        pageNum:1
      }
      listProductSoftwareRelease(params).then((res) =>{
        console.log("软件列表为",res)
        if(res.code === 200){
          if(res.rows.length > 0){
            for(var i=0;i<res.rows.length;i++){
              let temp = {
                value : res.rows[i].releaseCode,
                label : res.rows[i].releaseName
              }
              this.softWareCodeList.push(temp)
            }
          }
        }
      })
    },
    /** 获取硬件代码列表 */
    getHardWareList(){
      const params={
        pageSize:10000,
        pageNum:1
      }
      listProductHardwareInfo(params).then((res) =>{
        console.log("硬件列表为",res)
        if(res.code === 200){
          if(res.rows.length > 0){
            for(var i=0;i<res.rows.length;i++){
              let temp = {
                value : res.rows[i].hardwareCode,
                label : res.rows[i].hardwareName
              }
              this.hardWareCodeList.push(temp)
            }
          }
        }
      })
    },
    
    /** 查询生产计划列表 */
    getList() {
      this.loading = true;
      listProducePlan(this.queryParams).then(response => {
        this.ProducePlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        produceTotal: null,
        planProduceDate: null,
        productLineCode: null,
        productAddr: null,
        softwareCode: null,
        hardwareCode: null,
        expand1: null,
        expand2: null,
        planProduceCount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        removed: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProducePlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProducePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProducePlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产计划编号为"' + ids + '"的数据项？').then(function() {
          // 修改为逻辑删除
        return removedProducePlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('produce/produce-plan/export', {
        ...this.queryParams
      }, `produce-plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
